package com.gimi.test_jpa.com.gimi.service.customer;

import cn.hutool.core.bean.BeanUtil;
import com.gimi.test_jpa.com.gimi.dao.CustomerContactInfoDao;
import com.gimi.test_jpa.com.gimi.dto.customer.CustomerContactInfoDto;
import com.gimi.test_jpa.com.gimi.entity.customer.CustomerContactInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerContactInfoService {
    @Autowired
    private CustomerContactInfoDao contactInfoDao;

    public boolean createAndUpdate(CustomerContactInfoDto contactInfoDto) {
        CustomerContactInfoEntity entity = contactInfoDao.selectCusInfoById(contactInfoDto.getCusContactId());
        if (entity==null){
            entity=new CustomerContactInfoEntity();
            BeanUtil.copyProperties(contactInfoDto, entity);
            contactInfoDao.insert(entity);
            return true;
        }
        BeanUtil.copyProperties(contactInfoDto, entity);
        contactInfoDao.updateById(entity);
        return true;
    }

    public List<CustomerContactInfoDto> findbyCusPhone(String cusPhone,String cusEmail) {
        return contactInfoDao.findbyCusPhone(cusPhone,cusEmail);
    }
}
